package com.aranandroid.customview.squareview.base

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import androidx.annotation.Nullable

open class SquareView(
    @Nullable context: Context?,
    @Nullable attrs: AttributeSet?,
    defStyleAttr: Int,
    private val view: View
) {
    var squareNormalView: SquareNormalView
    var squareFocusedView: SquareFocusedView
    var squarePressedView: SquarePressedView

    fun refreshView() {
        view.setBackgroundDrawable(getBackGround())
        if (view is TextView) {
            if (squareNormalView.textColor != 0) {
                view.setTextColor(getColor())
            }
            view.setCompoundDrawables(
                getDraw("left"),
                getDraw("top"),
                getDraw("right"),
                getDraw("bottom")
            )
        }

    }

    init {
        squareNormalView = SquareNormalView(context, attrs)
        squareFocusedView = SquareFocusedView(context, attrs, squareNormalView)
        squarePressedView = SquarePressedView(context, attrs, squareNormalView)
        refreshView()
    }


    fun getBackGround(): StateListDrawable {
        val bg = StateListDrawable()
        val normal: Drawable = squareNormalView.getBackGround()
        val pressed: Drawable = squarePressedView.getBackGround()
        val focused: Drawable = squareFocusedView.getBackGround()
        bg.addState(intArrayOf(android.R.attr.state_pressed), pressed)
        bg.addState(intArrayOf(android.R.attr.state_focused), focused)
        bg.addState(intArrayOf(android.R.attr.state_selected), focused)
        bg.addState(intArrayOf(android.R.attr.state_checked), focused)
        bg.addState(intArrayOf(), normal)
        return bg
    }

    fun getDraw(type: String): StateListDrawable? {
        val draw = StateListDrawable()
        var normal: Drawable? = squareNormalView.leftDraw
        var pressed: Drawable? = squarePressedView.leftDraw
        var focused: Drawable? = squareFocusedView.leftDraw
        when (type) {
            "top" -> {
                normal = squareNormalView.topDraw
                pressed = squarePressedView.topDraw
                focused = squareFocusedView.topDraw
            }
            "bottom" -> {
                normal = squareNormalView.bottomDraw
                pressed = squarePressedView.bottomDraw
                focused = squareFocusedView.bottomDraw
            }
            "right" -> {
                normal = squareNormalView.rightDraw
                pressed = squarePressedView.rightDraw
                focused = squareFocusedView.rightDraw
            }
        }
        if (normal == null) {
            return null
        }
        if (pressed == null) {
            pressed = normal
        }
        if (focused == null) {
            focused = normal
        }
        draw.addState(intArrayOf(android.R.attr.state_pressed), pressed)
        draw.addState(intArrayOf(android.R.attr.state_focused), focused)
        draw.addState(intArrayOf(android.R.attr.state_selected), focused)
        draw.addState(intArrayOf(android.R.attr.state_checked), focused)
        draw.addState(intArrayOf(), normal)
        draw?.setBounds(0, 0, draw.getMinimumWidth(), draw.getMinimumHeight());
        return draw
    }

    fun getColor(): ColorStateList {
        val colors = intArrayOf(
            squarePressedView.textColor!!,
            squareFocusedView.textColor!!,
            squareFocusedView.textColor!!,
            squareFocusedView.textColor!!,
            squareNormalView.textColor!!
        )
        val states = arrayOfNulls<IntArray>(5)
        states[0] = intArrayOf(android.R.attr.state_pressed)
        states[1] = intArrayOf(android.R.attr.state_focused)
        states[2] = intArrayOf(android.R.attr.state_selected)
        states[3] = intArrayOf(android.R.attr.state_checked)
        states[4] = intArrayOf()
        return ColorStateList(states, colors)
    }


    /**
     * 正常
     * 左上 左下 右上 右下
     * 单位是px
     */
    fun setCorners(float: FloatArray) {
        squareNormalView.apply {
            cornerlt = float[0]
            cornerlb = float[1]
            cornerrt = float[2]
            cornerrb = float[3]
        }
        view.setBackgroundDrawable(getBackGround())
    }

    /**
     * 获取焦点
     * 左上 左下 右上 右下
     * 单位是px
     */
    fun setCornersFocused(float: FloatArray) {
        squareFocusedView.apply {
            cornerlt = float[0]
            cornerlb = float[1]
            cornerrt = float[2]
            cornerrb = float[3]
        }
        view.setBackgroundDrawable(getBackGround())
    }

    /**
     * 按下
     * 左上 左下 右上 右下
     * 单位是px
     */
    fun setCornersPressed(float: FloatArray) {
        squarePressedView.apply {
            cornerlt = float[0]
            cornerlb = float[1]
            cornerrt = float[2]
            cornerrb = float[3]
        }
        view.setBackgroundDrawable(getBackGround())
    }


}